package com.windystory.wslibrary.ui.update;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.azhon.appupdate.config.UpdateConfiguration;
import com.azhon.appupdate.dialog.NumberProgressBar;
import com.azhon.appupdate.listener.OnDownloadListener;
import com.azhon.appupdate.manager.DownloadManager;
import com.windystory.wslibrary.R;

import java.io.File;

/**
 * @author hhz
 * @time 2019/2/20 0020 10:04
 * @modify windystory
 */
public class VersionUpgradeDialog extends PopupWindow {
    public static final String TAG = "";
    private Activity mActivity;
    private View mMenuView;
    private ViewHolder viewHolder;

    private String title;
    private String message;
    private boolean isForce;
    private String apkName;
    private String apkUrl;
    private int icon;

    /**
     * @param activity
     */
    public VersionUpgradeDialog(Activity activity,
                                String title,
                                String message,
                                boolean isForce,
                                String apkName,
                                String apkUrl,
                                int icon) {
        super(activity);
        this.mActivity = activity;
        this.title = title;
        this.message = message;
        this.isForce = isForce;
        this.apkName = apkName;
        this.apkUrl = apkUrl;
        this.icon = icon;

        setClippingEnabled(false);

        LayoutInflater inflater = (LayoutInflater) mActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_version_upgrade, null);
        viewHolder = new ViewHolder();
        viewHolder.topImg = mMenuView.findViewById(R.id.top_img);
        viewHolder.title = mMenuView.findViewById(R.id.title);
        viewHolder.content = mMenuView.findViewById(R.id.content);
        viewHolder.cancelBtn = mMenuView.findViewById(R.id.cancel_btn);
        viewHolder.okBtn = mMenuView.findViewById(R.id.ok_btn);
        viewHolder.middleLayout = mMenuView.findViewById(R.id.middle_layout);
        viewHolder.bottomImg = mMenuView.findViewById(R.id.bottom_img);
        viewHolder.popLayout = mMenuView.findViewById(R.id.pop_layout);
        viewHolder.btnLayout = mMenuView.findViewById(R.id.btn_layout);
        viewHolder.progressBar = mMenuView.findViewById(R.id.progressBar);

        setContentView(mMenuView);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        ColorDrawable dw = new ColorDrawable(0x0000000);
        setBackgroundDrawable(dw);

        viewHolder.title.setText(title);
        viewHolder.content.setText(message);
        //强制更新
        if (isForce) {
            viewHolder.cancelBtn.setVisibility(View.GONE);
            setFocusable(false);
        } else {
            setFocusable(true);
            viewHolder.cancelBtn.setVisibility(View.VISIBLE);
        }

        viewHolder.cancelBtn.setOnClickListener(v -> dismiss());

        viewHolder.okBtn.setOnClickListener(v ->
        {
            if (isForce) {
                downloadForce();
            } else {
                downloadNormal();
                dismiss();
            }
        });
    }

    /**
     * 普通下载可忽略升级
     */
    private void downloadNormal() {
        DownloadManager manager = DownloadManager.getInstance(mActivity);
        UpdateConfiguration config = new UpdateConfiguration()
                .setJumpInstallPage(true);
        manager.setApkName(apkName)
                .setApkUrl(apkUrl)
                .setSmallIcon(icon)
                .setConfiguration(config)
                .download();
    }

    /**
     * 强制去下载
     */
    public void downloadForce() {
        DownloadManager manager = DownloadManager.getInstance(mActivity);
        UpdateConfiguration config = new UpdateConfiguration()
                .setJumpInstallPage(true)
                .setOnDownloadListener(new OnDownloadListener() {
                    @Override
                    public void start() {
                        viewHolder.okBtn.setText("下载中...");
                        viewHolder.okBtn.setEnabled(false);
                        viewHolder.cancelBtn.setEnabled(false);
                        viewHolder.progressBar.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void downloading(int max, int progress) {
                        viewHolder.progressBar.setMax(100);
                        int pro = (int) ((progress * 1.0 / max * 1.0) * 100.0);
                        viewHolder.progressBar.setProgress(pro);
                    }

                    @Override
                    public void done(File apk) {
                        viewHolder.okBtn.setText("立即安装");
                        viewHolder.okBtn.setEnabled(true);
                        viewHolder.cancelBtn.setEnabled(true);
                    }

                    @Override
                    public void cancel() {
                        viewHolder.okBtn.setText("重新下载");
                        viewHolder.okBtn.setEnabled(true);
                        viewHolder.cancelBtn.setEnabled(true);
                    }

                    @Override
                    public void error(Exception e) {
                        viewHolder.okBtn.setText("重新下载");
                        viewHolder.okBtn.setEnabled(true);
                        viewHolder.cancelBtn.setEnabled(true);
                    }
                });
        manager.setApkName(apkName)
                .setApkUrl(apkUrl)
                .setSmallIcon(icon)
                .setConfiguration(config)
                .download();
    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    public class ViewHolder {
        ImageView topImg;
        TextView title;
        TextView content;
        TextView cancelBtn;
        TextView okBtn;
        LinearLayout middleLayout;
        ImageView bottomImg;
        RelativeLayout popLayout;
        LinearLayout btnLayout;
        NumberProgressBar progressBar;
    }

    /**
     * 显示
     *
     * @param versionUpgradeDialog
     */
    public void show(VersionUpgradeDialog versionUpgradeDialog) {
        if (versionUpgradeDialog != null && mActivity != null)
            versionUpgradeDialog.showAtLocation(mActivity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }
}